package com.edou.system.controller.admin;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.simple.JSONObject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.edou.base.controller.BaseController;
import com.edou.system.dto.SysUploadFileCondition;
import com.edou.system.entity.SysUploadFile;
import com.edou.system.form.SysUploadFileForm;
import com.edou.system.service.ISysUploadFileService;

@Controller("sysUploadFileController")
@Scope("prototype")
@RequestMapping(value = "/admin/upload")
public class SysUploadFileController extends BaseController {

	private Log logger = LogFactory.getLog(SysUploadFileController.class);

	@Resource(name = "sysUploadFileServiceImpl")
	private ISysUploadFileService sysUploadFileService;

	/**
	 * 首页
	 * @return
	 */
	@RequestMapping(value = "/index.htm", method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView view = new ModelAndView();
		view.setViewName("");
		return view;
	}

	/**
	 * 分页查询
	 * @return
	 */
	@RequestMapping(value = "/listSysUploadFileByPage.htm", method = RequestMethod.POST)
	public void listSysUploadFileByPage(SysUploadFileForm model, HttpServletRequest request, HttpServletResponse response) {
		sysUploadFileService.listSysUploadFileByPage(model.getPageResult(), model.getCondition());
		JSONObject root = toPageJson(model.getPageResult(), new String[] {
			"id", "url", "name", "fileName", "extend" 
		});
		ajaxPageResult(root, response);
	}

	/**
	 * 分页查询
	 * @return
	 */
	@RequestMapping(value = "/list.htm", method = RequestMethod.POST)
	public ModelAndView list(SysUploadFileForm model,HttpServletRequest request, HttpServletResponse response) {
		
		ModelAndView view = new ModelAndView();
		view.setViewName("/admin/system/sys_photo_list_inc");
		Map<String, Object> data = view.getModel();
		
		SysUploadFileCondition condition = model.getCondition();
		
		String id = model.getObjectId();
		Integer type = model.getObjectType();
		Integer location = model.getLocation();
		
		condition.setObjectId(id);
		condition.setObjectType(type);
		condition.setLocation(location);
		
		if(StringUtils.isNotBlank(id)){
			// 根据广告位ID分页查询图片
			List<SysUploadFile> list = sysUploadFileService.listSysUploadFileByObjectIdAndTypeId(id, type.toString());
			model.getPageResult().setResult(list);
			data.put("pageResult", model.getPageResult());
		}
		
		data.put("objectId", id);
		data.put("objectType", type);
		data.put("condition", condition);
		
		return view;
		
	}

	/**
	 * 转到新增图片
	 * @return
	 */
	@RequestMapping(value = "/add.htm", method = RequestMethod.GET)
	public ModelAndView toAdd(SysUploadFileForm model,HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView();
		view.setViewName("/admin/system/sys_photo_add");
		Map<String, Object> data = view.getModel();
		
		SysUploadFile item = new SysUploadFile();
		item.setObjectId(model.getItem().getObjectId());
		item.setObjectType(model.getItem().getObjectType());
		
		data.put("item", item);
		
		return view;
	}
	
	/**
	 * 保存
	 * @return
	 */
	@RequestMapping(value = "/save.htm", method = RequestMethod.POST)
	public ModelAndView doSave(SysUploadFileForm model, HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView();
		view.setViewName("/admin/system/sys_photo_list_inc");
		Map<String, Object> data = view.getModel();
		SysUploadFile photo = model.getItem();
		try {
			// 如果图片名称为空时，将文件名默认为图片名称
			if (!StringUtils.isNotBlank(photo.getName())) {
				photo.setName(photo.getFileName());
			}
			// 如果排序为空时，默认排序为1
			if (null == photo.getSort()) {
				photo.setSort(1);
			}
			
			if(StringUtils.isNotBlank(photo.getId())){
				sysUploadFileService.update(photo);
			}else{
				sysUploadFileService.add(photo);
			}

			SysUploadFileCondition condition = model.getCondition();
			condition.setObjectId(photo.getObjectId());
			condition.setObjectType(photo.getObjectType());

			// 根据广告位ID分页查询图片
			sysUploadFileService.listSysUploadFileByPage(model.getPageResult(),condition);
			data.put("pageResult", model.getPageResult());
			return view;
		} catch (Exception e) {
			e.printStackTrace();
			String msg = "保存时发生异常：" + e.getMessage();
			logger.error(msg, e);
			return view;
		}
	}

	/**
	 * 转到修改图片页面
	 * @return
	 */
	@RequestMapping(value = "/update.htm", method = RequestMethod.GET)
	public ModelAndView toUpdate(SysUploadFileForm model, HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView();
		view.setViewName("/admin/system/sys_photo_add");
		Map<String, Object> data = view.getModel();
		SysUploadFile item = sysUploadFileService.getSysUploadFileById(model.getId());
		data.put("item", item);
		return view;
	}

	/**
	 * 删除
	 * @return
	 */
	@RequestMapping(value = "/del.htm", method = RequestMethod.POST)
	public ModelAndView del(SysUploadFileForm model, HttpServletRequest request, HttpServletResponse response) {
		try {
			if (StringUtils.isBlank(model.getSelIds())) {
				return ajaxJSON(Status.error, "ID不能为空");
			} else {
				sysUploadFileService.delByIds(model.getSelIds().split(","));
				return ajaxJSON(Status.success, "删除成功");
			}
		} catch (Exception e) {
			String msg = "删除时发生异常：" + e.getMessage();
			logger.error(msg, e);
			return ajaxJSON(Status.error, msg);
		}
	}


	/**
	 * 保存
	 * 
	 * @return
	 */
	@RequestMapping(value = "/doUpdate.htm", method = RequestMethod.POST)
	public ModelAndView doUpdate(SysUploadFileForm model, HttpServletRequest request, HttpServletResponse response) {
		SysUploadFile photo = model.getItem();
		try {
			SysUploadFile item = sysUploadFileService.getSysUploadFileById(photo.getId());
			// 如果图片名称为空时，将文件名默认为图片名称
			if (!StringUtils.isNotBlank(photo.getName())) {
				item.setName(photo.getFileName());
			} else {
				item.setName(photo.getName());
			}
			// 如果排序为空时，默认排序为1
			if (null == photo.getSort()) {
				item.setSort(1);
			}

			item.setDesc(photo.getDesc());
			item.setUrl(photo.getUrl());

			sysUploadFileService.update(item);
			return ajaxJSON(Status.success, "保存成功");
		} catch (Exception e) {
			String msg = "保存时发生异常：" + e.getMessage();
			logger.error(msg, e);
			return ajaxJSON(Status.error, msg);
		}
	}
	
	/**
	 * 跳转到图片审核与附件审核页面
	 * @return
	 */
	@RequestMapping(value = "/approve.htm", method = RequestMethod.GET)
	public ModelAndView approve(SysUploadFileForm model){
		Integer objectType = model.getObjectType();
		ModelAndView view = new ModelAndView();
		Map<String, Object> data = view.getModel();
		view.setViewName("/admin/system/sys_file_approve");
		data.put("objectType", objectType);
		return view;
	}
}
